package com.fesine.quartz.chapter4;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.impl.calendar.AnnualCalendar;

import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * @description: quartz中用于排除日期的日历calendar和java听calendar不同
 * @author: Fesine
 * @createTime:2016/11/18 21:37
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2016/11/18 21:37
 */
public class Listing_4_10 {
    static Log logger = LogFactory.getLog(Listing_4_10.class);

    public static void main(String[] args) {
        Listing_4_10 example = new Listing_4_10();
        example.startScheduler();
    }
    public void startScheduler() {
        try {
            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
            scheduler.start();
            scheduleJob(scheduler, PrintJob.class);
            logger.info("Scheduler starting up...");
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }

    private void scheduleJob(Scheduler scheduler, Class jobClass) {
        try {
            //获取quartz年日历实例
            AnnualCalendar cal = new AnnualCalendar();
            //获取java日历
            Calendar cd = GregorianCalendar.getInstance();
            //为日历设值月份-7
            cd.set(Calendar.MONTH, Calendar.JULY);
            //为日历设值日-4
            cd.set(Calendar.DATE, 4);
            //quartz日历设置排除日期
            cal.setDayExcluded(cd, true);//set排除日期
            //将quartz日历添加到调度器scheduler
            scheduler.addCalendar("bankHolidays", cal,true,true);
            //设置trigger
            Trigger trigger = TriggerUtils.makeImmediateTrigger("myTrigger", -1, 6000);
            trigger.setCalendarName("bankHolidays");
            //添加jobDetail
            JobDetail jobDetail = new JobDetail(jobClass.getName(), Scheduler.DEFAULT_GROUP,
                    jobClass);
            //将jobDetail和trigger添加到scheduler上
            scheduler.scheduleJob(jobDetail, trigger);
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
    }
}
